/* eslint-disable no-async-promise-executor */
import { APIURL } from "@/common/config/const";
import type { FixPanel } from "@/common/models/aigc-fix-panel";
import type { ChatFeedback, ChatHistory, RefrenceResource } from "@/common/models/chat-message";
import type { NavList } from "@/common/models/aigc-fix-panel";
import post, { get } from "@/common/services/request";
import type { ChatSession } from "@/common/models/session";
import type { EventSummary } from "@/common/models/event-summary";

const code = window.F10Utils.getUrlParams('code') || '300033';
const market = window.F10Utils.getUrlParams('market') || '33';
const engine = window.F10Utils.getUrlParams('engine') || 'default';

/**
 * 历史聊天记录请求
 * @param code 股票代码
 * @param market 市场id
 * @returns 
 */
export function getChatHistory(): Promise<ChatHistory> {
  return new Promise(async (resolve, reject) => {
    try {
      const res = await get({ url: APIURL.getChatHistory, data: { code, market, engine } });
      resolve(res.data);
    } catch (error) {
      reject(error);
    }
  })
}

/**
 * 顶部导航栏请求
 * @param code 股票代码
 * @param market 市场id
 * @returns 
 */
export function getNavList(): Promise<NavList> {
  return new Promise(async (resolve, reject) => {
    try {
      const res = await get({ url: APIURL.getNavList, data: { code, market, engine } });
      resolve(res.data);
    } catch (error) {
      reject(error);
    }
  })
}

/**
 * 面板数据请求（非流式）
 * @param pid 面板id
 */
export function getPanel(pid: string): Promise<FixPanel> {
  return new Promise(async (resolve, reject) => {
    try {
      const res = await get({ url: APIURL.getPanel, data: { pid, code, market, engine } });
      resolve(res.data);
    } catch (error) {
      reject(error);
    }
  })
}

/**
 * 重置面板
 * @param pid 面板id
 */
export function resetPanel(pid: string): Promise<FixPanel> {
  return new Promise(async (resolve, reject) => {
    try {
      const res = await get({ url: APIURL.resetPanel, data: { pid, code, market, engine } });
      if (res.status_code === 0) {
        resolve(res.data);
      } else {
        reject(res);
      }
    } catch (error) {
      reject(error);
    }
  })
}

/**
 * 更新面板标题
 * @param pid 面板id
 * @param title 标题
 */
export function updatePanelTitle(pid: string, title: string): Promise<FixPanel> {
  return new Promise(async (resolve, reject) => {
    try {
      const res = await get({ url: APIURL.updatePanelTitle, data: { pid, title, code, market, engine } });
      if (res.status_code === 0) {
        resolve(res.data);
      } else {
        reject(res.status_msg);
      }
    } catch (error) {
      reject(error);
    }
  })
}

/**
 * 自定义模块拼接数据请求
 * @param stock 股票名称
 * @param id 模块id
 */
export function getCustomeRender(stock: string, ids: string): Promise<any> {
  return new Promise(async (resolve, reject) => {
    try {
      const res = await get({ url: `${APIURL.getCustomeRender}?stock=${stock}&ids=${ids}` });
      resolve(res.data);
    } catch (error) {
      reject(error);
    }
  })
}

/**
 * 推荐问句
 * @param data 引入数据
 * @returns 
 */
export function getRecommendQuestion(data: RefrenceResource): Promise<{ questions: string[] }> {
  return new Promise(async (resolve, reject) => {
    try {
      const url = `${APIURL.getRecommendQuestion}?code=${code}&market=${market}&engine=${engine}`;
      const res = await post({ url, data });
      resolve(res.data);
    } catch (error) {
      reject(error);
    }
  })
}

/**
 * 点赞/点踩反馈
 * @param data 引入数据
 * @returns 
 */
export function feedback(data: ChatFeedback): Promise<any> {
  return new Promise(async (resolve, reject) => {
    try {
      const url = `${APIURL.feedback}?code=${code}&market=${market}&engine=${engine}`;
      const res = await post({ url, data });
      if (res.status_code === 0) {
        resolve(res.data);
      } else {
        reject(res.status_msg);
      }
    } catch (error) {
      reject(error);
    }
  })
}

/**
 * 导航栏Tab排序
 * @param tabs 排序后的tabs
 * @param appid 应用id
 * @returns 
 */
export function updateNavList(appid: string, tabs: string[]): Promise<any> {
  return new Promise(async (resolve, reject) => {
    try {
      const url = `${APIURL.updateNavList}?code=${code}&market=${market}&engine=${engine}`;
      const res = await post({ url, data: { 'app_id': appid, tabs } });
      if (res.status_code === 0) {
        resolve(res.data);
      } else {
        reject(res.status_msg);
      }
    } catch (error) {
      reject(error);
    }
  })
}

/**
 * 模块排序
 * @param pid 面板id
 * @param sids 排序后的sections
 * @returns 
 */
export function updateSectionSort(pid: string, sids: string[]): Promise<any> {
  return new Promise(async (resolve, reject) => {
    try {
      const url = `${APIURL.updateSectionSort}?code=${code}&market=${market}&engine=${engine}`;
      const res = await post({ url, data: { pid, sids } });
      if (res.status_code === 0) {
        resolve(res.data);
      } else {
        reject(res.status_msg);
      }
    } catch (error) {
      reject(error);
    }
  })
}



interface SectionStatus {
  ask_id?: string;
  pid?: string;
  sid: string;
  ext_title?: string;
  ext_visual_index?: number;
  ext_view_index?: number;
}
/**
 * 修改卡片状态
 * @param sid 卡片id
 * @param data 卡片状态数据
 * @returns 
 */
export function updateSectionStatus(data: SectionStatus): Promise<any> {
  return new Promise(async (resolve, reject) => {
    try {
      const url = `${APIURL.updateSectionStatus}?code=${code}&market=${market}&engine=${engine}`;
      const res = await get({ url, data });
      if (res.status_code === 0) {
        resolve(res.data);
      } else {
        reject(res.status_msg);
      }
    } catch (error) {
      reject(error);
    }
  })
}

/**
 * 插入卡片
 * @param ask_id 会话id
 * @param pid 面板id
 * @param sid 卡片id
 * @param after 插入在xx卡片之后
 * @returns 
 */
export function insertSection(ask_id: string, pid: string, sid: string, after?: string): Promise<any> {
  return new Promise(async (resolve, reject) => {
    try {
      const url = `${APIURL.insertSection}?code=${code}&market=${market}&engine=${engine}`;
      const res = await get({ url, data: { ask_id, pid, sid, after } });
      if (res.status_code === 0) {
        resolve(res.data);
      } else {
        reject(res.status_msg);
      }
    } catch (error) {
      reject(error);
    }
  })
}

/**
 * 删除卡片
 * @param pid 面板id
 * @param sid 卡片id
 * @returns 
 */
export function deleteSection(pid: string, sid: string): Promise<any> {
  return new Promise(async (resolve, reject) => {
    try {
      const url = `${APIURL.deleteSection}?code=${code}&market=${market}&engine=${engine}`;
      const res = await get({ url, data: { pid, sid } });
      if (res.status_code === 0) {
        resolve(res.data);
      } else {
        reject(res.status_msg);
      }
    } catch (error) {
      reject(error);
    }
  })
}

/**
 * 更新/替换卡片
 * @param ask_id 会话id
 * @param source_id 源卡片id
 * @param target_id 目标卡片id
 * @returns 
 */
export function updateSection(ask_id: string, source_id: string, target_id: string): Promise<any> {
  return new Promise(async (resolve, reject) => {
    try {
      const url = `${APIURL.updateSection}?code=${code}&market=${market}&engine=${engine}`;
      const res = await get({ url, data: { ask_id, source_id, target_id } });
      if (res.status_code === 0) {
        resolve(res.data);
      } else {
        reject(res.status_msg);
      }
    } catch (error) {
      reject(error);
    }
  })
}

/**
 * 清除上下文
 * @returns 
 */
export function resetChat(): Promise<ChatSession> {
  return new Promise(async (resolve, reject) => {
    try {
      const url = `${APIURL.resetChat}?code=${code}&market=${market}&engine=${engine}`;
      const res = await get({ url });
      if (res.status_code === 0) {
        resolve(res.data);
      } else {
        reject(res.status_msg);
      }
    } catch (error) {
      reject(error);
    }
  })
}

export function getRobotMessage(): Promise<EventSummary> {
  return new Promise(async (resolve, reject) => {
    try {
      const url = `https://flow.10jqka.com.cn/app/ai_timeline_summary_api/event/v2/list/${market}/${code}`
      const res = await get({ url });
      resolve(res.data);
    } catch (error) {
      reject(error);
    }
  })
}
